package com.cie2.map.pages;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.cie2.map.entities.Area;
import com.cie2.map.entities.AreaInMap;
import com.cie2.map.entities.MMap;
import com.cie2.map.services.AreaEngine;

/**
 * 
 * A page for creating a single map. But for the moment all of this is still manual where the user needs to 
 * - specify the center of the map
 * - what area exist around that center
 * 
 * for these things needs to be more automatic to minimize human error 
 * @author abangkis
 *
 */
public class CreateMap {
	
	@Component(id = "createMapForm")
	private Form _form;

	@Property
	private MMap mmap;
	
	@Property
	private List areaInMaps;
	
	@Property
	private AreaInMap areaInMap;
	
	@Property 
	private Area area;
	
	private boolean included;
	
	private List <Area> includedList;
	
	@Inject
	private AreaEngine areaEngine;
	
	void onPrepare() {
		areaInMaps = areaEngine.getAllArea();
	}
	
	void onValidateForm() {
		
	}
	
	@CommitAfter
	Object onSuccess() {
		System.out.println("=== count " + includedList.size());
		for (Iterator iterator = includedList.iterator(); iterator.hasNext();) {
			Area area = (Area) iterator.next();
			System.out.println(area.getName() + " ======= is included");
		}
		return CreateMap.class;
	}

	public List<Area> getAreaToInclude() {
		if (includedList == null) {
			includedList = new ArrayList<Area>();
		}
		return includedList;
	}

	
	// The Grid component will automatically call this for every row as it is rendered.
	public boolean isIncluded() {
		return included;
	}

	// The Grid component will automatically call this for every row on submit.
	public void setIncluded(boolean included) {
		if (included) {
			getAreaToInclude().add(area);
		}
	}

}
